package cn.qzuie.smart.module.dorm.service.bills;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.qzuie.smart.module.dorm.controller.admin.bills.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.bills.BillsDO;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;

import cn.qzuie.smart.module.dorm.dal.mysql.bills.BillsMapper;

import static cn.qzuie.smart.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.convertList;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.diffList;
import static cn.qzuie.smart.module.dorm.enums.ErrorCodeConstants.*;

/**
 * 账单 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BillsServiceImpl implements BillsService {

    @Resource
    private BillsMapper billsMapper;

    @Override
    public Long createBills(BillsSaveReqVO createReqVO) {
        // 插入
        BillsDO bills = BeanUtils.toBean(createReqVO, BillsDO.class);
        billsMapper.insert(bills);

        // 返回
        return bills.getId();
    }

    @Override
    public void updateBills(BillsSaveReqVO updateReqVO) {
        // 校验存在
        validateBillsExists(updateReqVO.getId());
        // 更新
        BillsDO updateObj = BeanUtils.toBean(updateReqVO, BillsDO.class);
        billsMapper.updateById(updateObj);
    }

    @Override
    public void deleteBills(Long id) {
        // 校验存在
        validateBillsExists(id);
        // 删除
        billsMapper.deleteById(id);
    }

    @Override
        public void deleteBillsListByIds(List<Long> ids) {
        // 删除
        billsMapper.deleteByIds(ids);
        }


    private void validateBillsExists(Long id) {
        if (billsMapper.selectById(id) == null) {
            throw exception(BILLS_NOT_EXISTS);
        }
    }

    @Override
    public BillsDO getBills(Long id) {
        return billsMapper.selectById(id);
    }

    @Override
    public PageResult<BillsDO> getBillsPage(BillsPageReqVO pageReqVO) {
        return billsMapper.selectPage(pageReqVO);
    }

}